﻿<?php

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Select;
use Phalcon\Forms\Element\Date;
use Phalcon\Forms\Element\TextArea;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Http\Request;

class NodeForm extends Form
{

    public function initialize($entity = null, $options = null)
    {
        $req = new Request();
        $genno = $req->getQuery('genno');
        $bind = $req->getQuery('bind');
        $id = null;$mode = 'A';
        $node = new Node();
        if($bind){            
            $tmp = explode('/',$bind);
            $id = $tmp[1];
            $mode = $tmp[0];
            $node = Node::findFirst('pers_id="'.$id.'"');
            if($node->birth_date == '0000-00-00') $node->birth_date = '';
            if($node->end_date == '0000-00-00') $node->end_date = '';
            //debug($tmp);
        }
        //$genno = isset($_GET['genno'])? $_GET['genno']:null;
        //  数据列ID
        $name = new Hidden('pers_id');
        $this->setVal($name,$node->pers_id);//修改
        $this->add($name);
        //  数据编辑模式
        $name = new Hidden('datamode');
        $this->setVal($name,$mode);//修改
        $this->add($name);

        // 姓名
        $name = new Text('name');
        $this->setVal($name,$node->name);//修改
        $this->add($name);

        //  字辈 
        //$genno = null;      
        if($genno){
            $name = new Select('zibei_no',
                Zibei::find('gen_no="'.$genno.'"'),   
                array(     
                    "using"=>array("zibei_no","zibei"),
                    "useEmpty" => true,
				    'emptyText' => '选择字辈'
                )
            );
            if($node->zibei_no) $name->setDefault($node->zibei_no);
        }else{
            $name = new Select('zibei_no',array(
                '' =>  '您没有创建字辈'
            ));
        }        
        $this->add($name);

        //  字辈隐藏域
        $name = new Hidden('zibei');
        $this->setVal($name,$node->zibei);
        $this->add($name);

        //  家谱标识
        $name = new Hidden('gen_no');
        $this->setVal($name,$genno);
        $this->add($name);

        // 性别
        $name = new Select(
            "sex",
            array(
                'M' => '男',
                'F' => '女'                
            )
        );
        if($node->sex) $name->setDefault($node->sex);
        $this->add($name);

        //  出生日期
        $name = new Date('birth_date');
        $this->setVal($name,$node->birth_date);
        $this->add($name);

        // 父
        $name = new Text('father_helper',array(
            'disabled'  => 'disabled'
        ));
        if($node->father){
            //$this->setVal($name,$this->getNodeName($node->father));
            $name->setAttributes(array(
                'disabled'  =>  'disabled', // readonly = readonly
                'value'     =>  $this->getNodeName($node->father)
            ));
        }
        $this->add($name);
        $name = new Hidden('father');
        $this->setVal($name,$node->father);
        $this->add($name);

        // 母
        $name = new Text('mother_helper',array(
            'disabled'  =>  'disabled'
        ));
        if($node->mother){
            //$this->setVal($name,$this->getNodeName($node->father));
            $name->setAttributes(array(
                'disabled'  =>  'disabled', // readonly = readonly
                'value'     =>  $this->getNodeName($node->mother)
            ));
        }
        $this->add($name);
        $name = new Hidden('mother');
        $this->setVal($name,$node->mother);
        $this->add($name);

        //  出生日期
        $name = new Date('end_date');
        $this->setVal($name,$node->end_date);
        $this->add($name);

        //  个人描述
        $name = new TextArea('desc');
        $this->setVal($name,$node->desc);
        $this->add($name);
    }
    //  简单的input设置值
    public function setVal($obj,$value)
    {
        if(is_object($obj)) $obj->setAttributes(array('value' => $value));
    }
    //获取父母的姓名
    public function getNodeName($id)
    {
        $node = Node::findFirst('pers_id="'.$id.'"');
        return $node->name;
    }
}